<script setup lang="ts">
import {computed, reactive} from 'vue'
import {Toast} from "vant";

const data: any = reactive({
  isActive: true, //控制标题
  title: "手机验证码登陆",
  regPhone: "", //手机号
  authCode: "", //验证码
  username: "", //用户名
  pwd: "", //密码
  agreement: false, //用户协议
  imgCode: "", //图片验证码
  isPwd: false,
  //60秒倒计时
  show: true,
  count: "",
  timer: null,
  flag: true, //该值变化，就会触发刷新
  code: "", //刷新后的验证码
  //图片验证码
  // 随机抽取四位数
  identifyCodes: "1234567890asdfghjklzxcvbnmqwertyuiop",
  identifyCode: "",
})
/*计算属性*/
const verifyPhone = computed(() => {
  let regular =
      /^1((34[0-8])|(8\d{2})|(([35][0-35-9]|4[579]|66|7[35678]|9[1389])\d{1}))\d{7}$/;
  return !regular.test(data.regPhone);
})
const verifyPwd = computed(() => {
  return data.pwd !== "";
})
/*获取验证码*/
const getCode = () => {
  //60秒倒计时
  const TIME_COUNT = 60;
  if (!data.timer) {
    data.count = TIME_COUNT;
    data.show = false;
    data.timer = setInterval(() => {
      if (data.count > 0 && data.count <= TIME_COUNT) {
        data.count--;
      } else {
        data.show = true;
        clearInterval(data.timer);
        data.timer = null;
      }
    }, 1000);
  }
}
/*表单校验*/
const onSubmit = () => {
  if (data.isActive) {
    /*手机号登陆*/
    if (verifyPhone) {
      Toast("手机号不正确");
      /*清空手机号*/
      data.regPhone = "";
      /*光标聚焦*/
      data.$refs.refphone.focus();
    } else if (!/^\d{4,6}$/.test(data.authCode)) {
      Toast("验证码不正确");
      /*清空验证码*/
      data.authCode = "";
      /*光标聚焦*/
      data.$refs.refcode.focus();
    } else if (!data.agreement) {
      Toast("请勾选用户协议");
      data.agreement = true;
    } else if (data.$route.query.path == undefined) {
      //用户存储
      localStorage.setItem("phone", JSON.stringify(data.regPhone));
      Toast.success("登陆成功");
      data.$router.push("/");
    } else {
      localStorage.setItem("phone", JSON.stringify(data.regPhone));
      Toast.success("登陆成功");
      data.$router.push(data.$route.query.path);
    }
  } else {
    /*密码登录*/
    if (data.username == "") {
      Toast("账号不能为空");
      /*光标聚焦*/
      data.$refs.refname.focus();
    } else if (data.pwd == "") {
      Toast("密码不能为空");
      /*光标聚焦*/
      data.$refs.refpwd.focus();
    } else if (data.imgCode === "") {
      Toast("验证码不能为空");
      /*光标聚焦*/
      data.$refs.refcode.focus();
    } else if (data.identifyCode !== data.imgCode) {
      Toast("验证码不正确");
      /*光标聚焦*/
      data.$refs.refcode.focus();
    } else {
      /*const axios = require("axios");*/
      /*data.$axios({
        method: "get",
        url: "/api/get/login",
        //URL中的查询参数,get请求用这个
        params: {
          username: data.username,
          pwd: data.pwd,
        },
      }).then((response) => {
        if (response.data.code == 200) {
          if (data.$route.query.path == undefined) {
            //用户存储
            localStorage.setItem("username", JSON.stringify(data.username));
            Toast.success("登陆成功");
            data.$router.push("/");
          } else {
            localStorage.setItem("username", JSON.stringify(data.username));
            Toast.success("登陆成功");
            data.$router.push(data.$route.query.path);
          }
        } else {
          Toast.fail("用户名密码不存在");
          data.username = "";
          data.pwd = "";
          data.imgCode = "";
          data.$refs.refname.focus();
        }
      });*/
    }
  }
}
</script>

<template>
  <div class="login">
    <header class="header">
      <button>关闭</button>
      <div class="title">
        <strong>{{ data.title }}</strong>
        <a href="#" @click="data.isActive=false" :class="{ active: !data.isActive }">账号密码登陆</a>
        <a href="#" @click="data.isActive=true" :class="{ active: data.isActive }">手机号验证码登陆</a>
      </div>
    </header>
    <section class="main">
      <!--@submit.prevent="onsubmit"-->
      <form @submit.prevent="onSubmit">
        <!--登陆-->
        <article v-if="data.isActive" class="regsiter">
          <ul>
            <!--手机号登陆-->
            <li>
              <input
                  type="text"
                  oninput="if(value.length > 11)value = value.slice(0, 15)"
                  placeholder="请输入手机号"
                  ref="refphone"
                  v-model="data.regPhone"/>
              <span>+86</span>
            </li>
            <!--获取验证码-->
            <li>
              <input
                  type="text"
                  placeholder="请输入验证码"
                  ref="refcode"
                  v-model="data.authCode"/>
              <button
                  type="button"
                  :disabled="verifyPhone"
                  :class="[!verifyPhone && data.show ? 'truebtn' : 'falsebtn']"
                  @click="getCode">
                {{ !data.show ? `${data.count}后发送验证码` : `获取验证码` }}
              </button>
            </li>
            <!--文字说明-->
            <li>
              <strong>未注册手机号验证后自动自动创建账号</strong>
              <a href="#">语音验证码</a>
            </li>
            <!--勾选协议-->
            <li>
              <em>
                <van-checkbox v-model="data.agreement" icon-size="30">我已阅读并同意
                  <a href="#">《用户协议》、《隐私用户》</a></van-checkbox>
              </em>
            </li>
          </ul>
        </article>
        <!--账号登陆-->
        <article v-else class="regsiter">
          <ul>
            <!--账号登陆-->
            <li>
              <input type="text"
                     oninput="if(value.length > 11)value = value.slice(0, 15)"
                     placeholder="请输入手机号/邮箱"
                     ref="refname"
                     v-model="data.username"/>
            </li>
            <!--输入密码-->
            <li>
              <input :type="data.isPwd ? 'text' : 'password'"
                     placeholder="请输入密码"
                     v-model="data.pwd"
                     ref="refpwd"/>
              <button v-show="verifyPwd"
                      type="button"
                      class="yanjing"
                      @click="data.isPwd = !data.isPwd"
                      :class="['iconfont', data.isPwd ? 'icon-yanjing' : 'icon-biyan--']"
              ></button>
            </li>
            <!--图片验证码-->
            <li>
              <input type="text"
                     placeholder="请输入验证码"
                     ref="refcode"
                     v-model="data.imgCode"/>
              <div class="code">
                <!--@click="refreshCode"-->
                <!--<identify :identifyCode="identifyCode"></identify>-->
              </div>
            </li>
          </ul>
        </article>
        <!--登陆按钮-->
        <button type="submit">登陆</button>
        <dl>
          <dt>一键登录</dt>
          <dt>密码登陆</dt>
        </dl>
      </form>
    </section>
    <footer class="footer">
      <i class="iconfont icon-shejiaotubiao-06"></i>
      <i class="iconfont icon-weixin"></i>
      <i class="iconfont icon-qq"></i>
    </footer>
  </div>
</template>

<style lang="scss" scoped>

$bgColor: #ffffff; //背景颜色
$color: #adaabb; //默认字体颜色
$activeColor: #16a87b; //字体高亮颜色
//设置input框输入高亮
textarea:focus,
input[type="text"]:focus,
input[type="datetime"]:focus,
input[type="password"]:focus,
input[type="number"]:focus {
  border-color: rgba(82, 168, 236, 0.8);
  outline: 0;
  outline: thin dotted \9
;
  -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075),
  0 0 8px rgba(82, 168, 236, 0.6);
  -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075),
  0 0 8px rgba(82, 168, 236, 0.6);
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075),
  0 0 8px rgba(82, 168, 236, 0.6);
}

.login {
  width: 100%;
  height: calc(100% - 1px);
  min-width: 320px;
  max-width: 750px;
  padding: 60px;
  font-size: 28px;
  background-color: #fff;

  .header {
    width: 100%;
    height: 300px;

    button {
      width: 78px;
      height: 34px;
      color: $color;
      font-size: 32px;
      background-color: #fff;
      border: none;
      margin-bottom: 60px;
    }

    .title {
      height: 200px;
      text-align: center;
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
      box-sizing: border-box;
      padding: 0 20px;

      strong {
        width: 100%;
        font-size: 46px;
        font-weight: bold;
      }

      a {
        margin-top: 50px;
        height: 40px;
      }

      .active {
        color: orange;
        border-bottom: orange solid 1px;
      }
    }
  }

  .main {
    height: auto;

    .regsiter,
    .login {
      height: auto;
      font-size: 28px;
    }

    .regsiter {
      ul {
        li {
          position: relative;
          margin-top: 50px;
          display: flex;
          justify-content: space-between;
          box-sizing: border-box;

          input {
            box-sizing: border-box;
            width: 100%;
            height: 90px;
            border-radius: 45px;
            padding-left: 35px;
            border: 0;
            font-size: 32px;
            background-color: #eff0f4;
          }

          span,
          button,
          .code {
            position: absolute;
            top: 25px;
            right: 46px;
            width: 90px;
            height: 40px;
            line-height: 40px;
            font-size: 35px;
          }

          .code {
            margin-top: -25px;
            right: 40px;
            color: $color;
            width: 200px;
            height: 60px;
            line-height: 60px;
            text-align: center;
            z-index: 2;
          }

          span,
          strong {
            color: $color;
          }

          strong {
            font-size: 24px;
            margin-left: 15px;
          }

          em {
            width: 100%;
            box-sizing: border-box;
            font-size: 26px;
            padding: 25px;
          }

          span {
            &:after {
              position: absolute;
              display: inline-block;
              top: 6px;
              right: 0px;
              border-left: 20px solid $color;
              border-bottom: 20px solid $color;
              border-color: transparent $color $color transparent;
              content: "";
              transform: rotate(45deg);
            }
          }

          .falsebtn,
          .truebtn {
            text-align: right;
            width: 260px;
            height: 60px;
            right: 46px;
            font-size: 33px;
            line-height: 60px;
            color: $color;
            top: -10px;
            background-color: #eff0f4;
          }

          .yanjing {
            color: $color;
            background-color: #eff0f4;
            font-size: 52px;
            top: 2px;
          }

          .truebtn {
            color: $activeColor;
          }

          button {
            width: 160px;
            border: none;
            top: 10px;
            right: 0px;
            border-radius: 30px;
          }

          a {
            color: $activeColor;
            font-size: 24px;
            margin-right: 15px;
          }
        }
      }
    }

    button {
      width: 100%;
      height: 90px;
      border-radius: 45px;
      border: none;
      font-size: 35px;
      color: #fff;
      margin-top: 20px;
      background-color: $color;
    }

    dl {
      width: 100%;
      height: 36px;
      margin-top: 40px;
      display: flex;
      justify-content: center;
      color: #6c737b;

      dt:first-child {
        border-right: 1px solid $color;
      }

      dt {
        padding: 0 20px;
      }
    }
  }

  .footer {
    margin-top: 190px;
    width: 100%;
    display: flex;
    justify-content: space-around;

    i {
      width: 78px;
      height: 78px;
      border-radius: 100%;
      line-height: 78px;
      text-align: center;
      font-size: 55px;
      background-color: #f6f7fb;
    }

    i:first-child {
      color: #fb7573;
    }

    i:nth-child(2) {
      color: #00cda2;
    }

    i:last-child {
      color: #489bf9;
      position: relative;
    }
  }
}
</style>